/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UpdateStock1.java
 *
 * Created on Nov 10, 2011, 5:06:46 PM
 */
package Manager.Update;

import Entity.Barang;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author wienda
 */
public class UpdateStock extends javax.swing.JPanel {

    List<Barang> records = new ArrayList<Barang>();
    UpdateController upd;

    /** Creates new form UpdateStock1 */
    public UpdateStock() {
        initComponents();
        upd = new UpdateController();
        this.statusAwal();
        TabelBarang.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                int row = TabelBarang.getSelectedRow();
                if (row != -1) {
                    isiTeks();
                }
            }
        });
    }

    void isiTabel() {
        Object data[][] = new Object[records.size()][6];
        int x = 0;
        for (Barang b : records) {
            data[x][0] = b.getKodebarang();
            data[x][1] = b.getNamabarang();
            data[x][2] = b.getJenisbarang();
            data[x][3] = b.getStock();
            data[x][4] = b.getHargabarang();
            x++;
        }
        String judul[] = {"Kode Barang", "Nama Barang", "Jenis Barang", "Stock", "Harga Barang"};
        TabelBarang.setModel(new DefaultTableModel(data, judul));
    }

    void kosongkanTeks() {
        kodeText.setText("");
        namaText.setText("");
        comboJenisBarang.setSelectedIndex(0);
        StokText.setText("");
        Hargatf.setText("");
    }

    void isiTeks() {
        Barang b = records.get(TabelBarang.getSelectedRow());
        kodeText.setText(b.getKodebarang());
        namaText.setText(b.getNamabarang());
        comboJenisBarang.setSelectedItem(b.getJenisbarang());
        StokText.setText(String.valueOf(b.getStock()));
        Hargatf.setText(String.valueOf(b.getHargabarang()));
        UpdateButton.setEnabled(true);
        TambahButton.setEnabled(false);
        namaText.setEditable(false);
        kodeText.setEditable(false);
    }

    void loadBarang() {
        try {
            records = upd.getAll();
        } catch (SQLException ex) {
            Logger.getLogger(UpdateStock.class.getName()).log(Level.SEVERE, null, ex);
        }catch (UpdateException ex) {
            Logger.getLogger(UpdateStock.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    void statusAwal() {
        this.kosongkanTeks();
        this.loadBarang();
        this.isiTabel();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        kodeText = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        StokText = new javax.swing.JTextField();
        TambahButton = new javax.swing.JButton();
        UpdateButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        TabelBarang = new javax.swing.JTable();
        comboJenisBarang = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        namaText = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        Hargatf = new javax.swing.JTextField();
        batalButton = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "FORM UPDATE STOK", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.ABOVE_TOP, new java.awt.Font("Times New Roman", 1, 24))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setText("Kode Barang");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setText("Nama Barang");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel3.setText("Stok Barang");

        TambahButton.setText("Tambah");
        TambahButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TambahButtonActionPerformed(evt);
            }
        });

        UpdateButton.setText("Update");
        UpdateButton.setEnabled(false);
        UpdateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                UpdateButtonActionPerformed(evt);
            }
        });

        TabelBarang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Kode Barang", "Nama Barang", "Jenis Barang", "Stok", "Harga Barang"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(TabelBarang);

        comboJenisBarang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Makanan", "Minuman", "Boneka", "Pakaian", "Alat Tulis Kantor", "Alat Rumah Tangga" }));

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel5.setText("Jenis Barang");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel4.setText("Harga Barang");

        batalButton.setText("Batal");
        batalButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                batalButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(55, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 480, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(54, 54, 54))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(jLabel5)
                                            .addGap(3, 3, 3))
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel2)
                                            .addComponent(jLabel1)))
                                    .addComponent(jLabel3)
                                    .addComponent(TambahButton))
                                .addGap(96, 96, 96)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(UpdateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(batalButton))
                                    .addComponent(Hargatf)
                                    .addComponent(kodeText, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                                    .addComponent(StokText, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)
                                    .addComponent(namaText)
                                    .addComponent(comboJenisBarang, 0, 187, Short.MAX_VALUE))))
                        .addContainerGap(139, Short.MAX_VALUE))))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 294, Short.MAX_VALUE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 295, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(kodeText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(namaText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboJenisBarang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(StokText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Hargatf, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TambahButton)
                    .addComponent(batalButton)
                    .addComponent(UpdateButton))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 189, Short.MAX_VALUE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 189, Short.MAX_VALUE)))
        );
    }// </editor-fold>//GEN-END:initComponents

private void TambahButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TambahButtonActionPerformed
    if (namaText.getText().equals("") && StokText.getText().equals("") && Hargatf.getText().equals("")) {
        JOptionPane.showMessageDialog(null, "Isi form diatas dengan lengkap");
    } else {
        {
            try {
                Barang b = new Barang();
                b.setKodebarang(kodeText.getText());
                b.setNamabarang(namaText.getText());
                b.setJenisbarang(comboJenisBarang.getSelectedItem().toString());
                b.setStock(Integer.parseInt(StokText.getText()));
                b.setHargabarang(Double.parseDouble(Hargatf.getText()));
                upd.insert(b);
                JOptionPane.showMessageDialog(null, "Data Tersimpan");

                this.statusAwal();
            } catch (UpdateException ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage());
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage());
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this, "stok dan harga diisi dengan angka");
            }
        }

    }

}//GEN-LAST:event_TambahButtonActionPerformed

private void UpdateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_UpdateButtonActionPerformed
// TODO add your handling code here:
    try {
        Barang b = new Barang();
        b.setKodebarang(kodeText.getText());
        b.setNamabarang(namaText.getText());
        b.setJenisbarang(comboJenisBarang.getSelectedItem().toString());
        if (StokText.getText().charAt(0) == '+') {
            b.setStock(((Integer) TabelBarang.getValueAt(TabelBarang.getSelectedRow(), 3)).intValue() + Integer.parseInt(StokText.getText().substring(1)));
        } else if (StokText.getText().charAt(0) == '-') {
            b.setStock(((Integer) TabelBarang.getValueAt(TabelBarang.getSelectedRow(), 3)).intValue() - Integer.parseInt(StokText.getText().substring(1)));
        } else {
            b.setStock(Integer.parseInt(StokText.getText()));

        }
        b.setHargabarang(Double.parseDouble(Hargatf.getText()));
        upd.update(b);
        UpdateButton.setEnabled(false);
        TambahButton.setEnabled(true);
        namaText.setEditable(true);
        kodeText.setEditable(true);
        this.statusAwal();
    } catch (UpdateException ex) {
        JOptionPane.showMessageDialog(this, ex.getMessage());
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage());
    } catch (NumberFormatException ex) {
        JOptionPane.showMessageDialog(null, "harga dan stok diisi dengan angka");
    }

}//GEN-LAST:event_UpdateButtonActionPerformed

private void batalButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_batalButtonActionPerformed
    this.kosongkanTeks();
    UpdateButton.setEnabled(false);
    TambahButton.setEnabled(true);
    namaText.setEditable(true);
    kodeText.setEditable(true);
}//GEN-LAST:event_batalButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField Hargatf;
    private javax.swing.JTextField StokText;
    private javax.swing.JTable TabelBarang;
    private javax.swing.JButton TambahButton;
    private javax.swing.JButton UpdateButton;
    private javax.swing.JButton batalButton;
    private javax.swing.JComboBox comboJenisBarang;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField kodeText;
    private javax.swing.JTextField namaText;
    // End of variables declaration//GEN-END:variables
}
